package dao

import (
	"go_payment/app/param_model"
	"go_payment/model"
)

func GetProductUser(memberId, bankCode int) *model.ProductUser {
	sql := "SELECT userid,pid,channel,polling,weight FROM pay_product_user WHERE `userid`=? AND `pid`=? AND `status`=1 LIMIT 1"
	stmt, err := db.Prepare(sql)
	defer stmt.Close()
	if err != nil {
		panic(err)
	}
	row := stmt.QueryRow(memberId, bankCode)
	var productUser model.ProductUser
	row.Scan(&productUser.UserId, &productUser.ProductId, &productUser.Channel, &productUser.Polling, &productUser.Weight)
	return &productUser
}

func GetProductUserConf(memberId, bankId int, trade param_model.Trade) []model.ProductUserConf {
	sql := "SELECT `id`,`mch_id`,`signkey`,`appid`,`appsecret`,`domain` FROM pay_product_user_config WHERE `userid`=? AND `bankid`=? AND `pid`=? AND `status`=1"
	stmt, err := db.Prepare(sql)
	defer stmt.Close()
	if err != nil {
		panic(err)
	}
	rows, err := stmt.Query(memberId, bankId, trade.BankCode)
	if err != nil {
		panic(err)
	}
	configs := make([]model.ProductUserConf, 0)
	if rows.Next() {
		var conf model.ProductUserConf
		rows.Scan(&conf.ID, &conf.MchID, &conf.SignKey, &conf.AppId, &conf.AppSecret, &conf.Domain)
		configs = append(configs, conf)
	}
	return configs
}
